const weeks = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日']
Page({
  data: {
    tableData: [
    ],
    isShowHead: false
  },
  onShow () {
    const userInfo = wx.getStorageSync('userInfo')
    let bool;
    if (userInfo.identityType == 4) {
      bool = true
    } else {
      bool = false
    }
    this.setData({
      isShowHead: bool
    })
    this.getCourse()
  },
  /**
   * @see 打开我的课件
   */
  openMyCourse () {
    // myCourseware
    wx.navigateTo({
      // url: `/pages/hdctWeb/hdctWeb?path=http://192.168.0.5:3000/#/interactiveClassRoom/teacher/myCourseware`,  // 本地开发环境ip根据实际情况更换
      url: `/pages/hdctWeb/hdctWeb?path=https://xwx.gzzmedu.com:6899/x-mai-hdct/#/interactiveClassRoom/teacher/myCourseware`,
    })
  },
  /**
   * @see 获取课程
   */
  getCourse () {
    return new Promise(resolve => {
      const userInfo = wx.getStorageSync('userInfo')
      let key = userInfo.identityType === 4 ? 'getTeacherCourse' : 'getStudentCourse';
      wx.$API[`home/${key}`]({}).then((res) => {
        res.data.forEach(v => {
          v.weekText = weeks[v.dayTime]
        })
        this.setData({
          tableData: res.data
        })
        resolve()
      }).catch(error => {
        wx.showToast({
          title: error,
          icon: 'none'
        })
        resolve()
      })
    })
  },
  /**
   * @see 置顶事件
   */
  stickyCurrent (courseId) {
    let { tableData } = this.data
    let obj = {}
    for (let i = 0; i < tableData.length; i++) {
      if (tableData[i].courseId == courseId) {
        obj = tableData[i]
        tableData.splice(i, 1)
      }
    }
    tableData.unshift(obj)
    this.setData({
      tableData: tableData
    })
  },
  /**
   * @see 弹出选项
   */
  handleStick (e) {
    const stickIndex = e.currentTarget.dataset.index
    const stickItem = e.currentTarget.dataset.item
    wx.showActionSheet({
      itemList: ['置顶'],
      itemColor: '#333333',
      success: (res) => {
        if (res.tapIndex === 0) {
          this.stickyCurrent(stickItem.courseId)
        }
      },
      fail (res) {
        console.log('取消', res)
      }
    })
  },
  /**
   * @see 点击更多
   */
  handleMore (e) {
    const moreIndex = e.currentTarget.dataset.index
    const mmoreItem = e.currentTarget.dataset.item
  },
  /**
   * @see 点击跳转到互动课堂H5
   */
  openHdct (e) {
    wx.navigateTo({
      // url: `/pages/hdctWeb/hdctWeb?courseId=${e.currentTarget.dataset.item.courseId}&path=http://192.168.6.169:3000/#/interactiveClassRoom/teacher/classRoomInfo&title=${e.currentTarget.dataset.item.courseName}`, // 本地开发环境 ip根据实际情况更换
      url: `/pages/hdctWeb/hdctWeb?courseId=${e.currentTarget.dataset.item.courseId}&path=https://xwx.gzzmedu.com:6899/x-mai-hdct/#/interactiveClassRoom/teacher/classRoomInfo&title=${e.currentTarget.dataset.item.courseName}`,
    })
  },
  onPullDownRefresh(){
    wx.showNavigationBarLoading() //在标题栏中显示加载
    const userInfo = wx.getStorageSync('userInfo')
    wx.$API['home/refreshCourse']({
      identityType:userInfo.identityType
    }).then(res => {
      this.getCourse().then(res => {
        wx.stopPullDownRefresh() //停止下拉刷新
        wx.hideNavigationBarLoading() //完成停止加载
      })
    })
  }
})